#include "netprotocol.h"
#include "netdriver.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QDebug>
#include <QMessageBox>
#include "datatemp.h"
#include <QJsonArray>
netProtocol *netProtocol::obj = nullptr;
netProtocol::netProtocol(QObject *parent) : QObject(parent)

{
connect(netDriver::getObject(),&netDriver::netRead,this,&netProtocol::netReadSlot);
}

netProtocol *netProtocol::getObject()

{
if(obj == nullptr)
    obj = new netProtocol;
return obj;
}

bool netProtocol::userfLogin(QString username, QString password)
{
  if(!netDriver::getObject()->netGetState())
  {
      qDebug() << "disconnect error:1";
      return false;
  }
  QJsonObject obj;
  obj.insert("type",5001);
  obj.insert("username",username);
  obj.insert("password",password);
  return netDriver::getObject()->netWrite(QJsonDocument(obj).toJson());
}


bool netProtocol::controlNormalDevice(QString index, int id, bool state)
{
    if(!netDriver::getObject()->netGetState())
    {
        qDebug() << "disconnect error:4";
        return false;
    }
    QJsonObject obj;
    obj.insert("type",2003);
    obj.insert("secret_key",dataTemp::getObject()->secretKey);
    obj.insert("index",index);
   obj.insert("id",id);// 1 b1w1  2b1w2 3 b2w1 4 b2w2
   obj.insert("sw",state);
   return netDriver::getObject()->netWrite(QJsonDocument(obj).toJson());
}

bool netProtocol::requestMessage(int type)
{
    if(!netDriver::getObject()->netGetState())
    {
        qDebug() << "disconnect error:5";
        return false;
    }
    QJsonObject obj;
    obj.insert("type",3001);
    obj.insert("secret_key",dataTemp::getObject()->secretKey);
    obj.insert("data_type",type);
    obj.insert("device_id",dataTemp::getObject()->currentDeviceId);

    return netDriver::getObject()->netWrite(QJsonDocument(obj).toJson());
}

bool netProtocol::requestMyDeviceName()
{
    if(!netDriver::getObject()->netGetState())
    {
        qDebug() << "disconnect error:6";
        return false;
    }
    QJsonObject obj;
    obj.insert("type",6001);
    obj.insert("secret_key",dataTemp::getObject()->secretKey);
return netDriver::getObject()->netWrite(QJsonDocument(obj).toJson());

}





void netProtocol::netReadSlot(QByteArray data)
{
    int count = 0;
    for(int i = 0; i < data.length(); i++) {
        if(data.at(i) == '{')
            count++;
        else if(data.at(i) == '}') {
            count--;
            if(count == 0) {
                QByteArray temp = data.mid(0, i + 1);
                handleFrame(temp);
                data = data.mid(i+1);
                i = -1;
            }
        }
    }
}
void netProtocol::handleFrame(const QByteArray &frame)
{
    QJsonObject obj = QJsonDocument::fromJson(frame).object();
    qDebug() << obj;
    int type = obj.value("type").toInt();
    if(type == 5001)
    {
       bool ok= obj.value("re").toBool();
       if(ok){
   dataTemp::getObject()->secretKey = obj.value("secret_key").toString();

       qDebug() << "登录成功";

       }
       else {
           qDebug() << "登录失败";
            }
       emit LoginFeedBack(ok);
       requestMyDeviceName();
    }
    qDebug() << "secretKey=" << dataTemp::getObject()->secretKey;
    if(type == 6001)
    {
    QJsonArray arr;
    arr = obj.value("arr").toArray();
    emit updateMyDeviceName(arr);
    }
    if(type == 3001)
    {
    QJsonArray arr;
    arr= obj.value("arr").toArray();

    emit showDodayMessage(arr);
    }
    if(type == 1001)
    emit realTimeMessage(obj);
}
